//
//  ZDShareView.m
//  SSSMerchant
//
//  Created by Darker on 2017/3/23.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "ZDShareView.h"

@implementation ZDShareView

- (instancetype)initWithFrame:(CGRect)frame dataUrl:(NSString *)dataUrl imageUrl:(NSString *)imageUrl textTitle:(NSString *)title content:(NSString *)content startVc:(UIViewController *)startVc{
    if (self = [super initWithFrame:frame]) {
        _title = title;
        _dataUrl = dataUrl;
        _imageUrl = imageUrl;
        _content = content;
        _startVc = startVc;
        self.backgroundColor = [UIColor clearColor];
        [self addButtonsOnSecondCell];
        //        [self addguester];
    }
    return self;
}

/**
 *  向背景视图添加按钮
 *
 */
- (void)addButtonsOnSecondCell {
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, KSCREEN_Height - HeigthIos6CompeteWithCurrent(241), KSCREEN_Width, HeigthIos6CompeteWithCurrent(241))];
    bgView.backgroundColor = [UIColor colorWithHex:@"E6E6E6"];
    [self addSubview:bgView];
    NSArray *titles = @[@"微信好友",@"朋友圈",@"QQ好友",@"QQ空间"];
    NSArray *names = @[@"weichat",@"momment",@"qq",@"qqzone"];
    NSArray *titleColor = @[@"40B87B",@"40B87B",@"26B8F3",@"26B8F3"];
    UIButton *__block preButton = nil;
    [names enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [bgView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@(WidthIos6CompeteWithCurrent(45)));
            if (idx < 2) {
                make.top.equalTo(bgView.mas_top).offset(HeigthIos6CompeteWithCurrent(20));
                if (idx == 0) {
                    make.left.equalTo(bgView.mas_left).offset(HeigthIos6CompeteWithCurrent(76));
                }
                else {
                    make.left.equalTo(preButton.mas_right).offset(HeigthIos6CompeteWithCurrent(116));
                }
            }
            else {
                make.top.equalTo(bgView.mas_top).offset(HeigthIos6CompeteWithCurrent(98));
                if (idx == 2) {
                    make.left.equalTo(bgView.mas_left).offset(HeigthIos6CompeteWithCurrent(76));
                }
                else {
                    make.left.equalTo(bgView.mas_left).offset(HeigthIos6CompeteWithCurrent(76)  + HeigthIos6CompeteWithCurrent(116) + WidthIos6CompeteWithCurrent(45));
                }
            }
            UILabel *titleLabel = [[UILabel alloc] init];
            titleLabel.font = [SSSFont systemFontOfSize:15];
            titleLabel.textColor = [UIColor colorWithHex:titleColor[idx]];
            [bgView addSubview:titleLabel];
            titleLabel.text = titles[idx];
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(button.mas_bottom).offset(10);
                make.centerX.equalTo(button.mas_centerX);
            }];
            preButton = button;
        }];
        [button setImage:[UIImage imageNamed:obj] forState:UIControlStateNormal];
        button.tag = 30000 + idx;
        [button addTarget:self action:@selector(shareToSomeplace:) forControlEvents:UIControlEventTouchDown];
    }];
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, bgView.frame.size.height - 39 , bgView.frame.size.width, 39)];
    cancelButton.layer.borderWidth = 0.5;
    cancelButton.layer.borderColor = [UIColor colorWithHex:@"666666"].CGColor;
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor colorWithHex:@"000000"] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonClicked) forControlEvents:UIControlEventTouchDown];
    [bgView addSubview:cancelButton];

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self removeFromSuperview];
    
}
- (void)cancelButtonClicked {
    self.alpha = 0;
}
/**
 *  分享到各大平台
 */
- (void)shareToSomeplace:(UIButton *)sender {
    //    static NSInteger shareName = 0;
    //    shareName = sender.tag - 30000;
    //    NSArray *names = @[UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,UMShareToQzone,UMShareToSina];
    //    //需要自定义面板样式的开发者需要自己绘制UI，在对应的分享按钮中调用此接口
    //    [UMSocialData defaultData].extConfig.title = self.title;
    //    //    UMSocialUrlResource *urlResource = [[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeImage url:
    //    //                                        _imageUrl];
    //    [UMSocialData openLog:YES];
    //    [UMSocialData defaultData].extConfig.qqData.url = _dataUrl;
    //    [UMSocialData defaultData].extConfig.qzoneData.url = _dataUrl;
    //    [UMSocialData defaultData].extConfig.wechatSessionData.url = _dataUrl;
    //    [UMSocialData defaultData].extConfig.wechatTimelineData.url = _dataUrl;
    //    [UMSocialData defaultData].extConfig.sinaData.urlResource.url = _dataUrl;
    //    if (shareName == 4) {
    //        [[UMSocialDataService defaultDataService]  postSNSWithTypes:[NSArray arrayWithObject:names[shareName]] content:[NSString stringWithFormat:@"%@%@",self.title,_dataUrl] image:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_imageUrl] options:0 error:nil]] location:nil urlResource:nil presentedController:_startVc completion:^(UMSocialResponseEntity *shareResponse){
    //            if (shareResponse.responseCode == UMSResponseCodeSuccess) {
    //
    //            }
    //        }];
    //    }
    //    else {
    //        [[UMSocialDataService defaultDataService]  postSNSWithTypes:[NSArray arrayWithObject:names[shareName]] content:_content image:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_imageUrl] options:0 error:nil]] location:nil urlResource:nil presentedController:_startVc completion:^(UMSocialResponseEntity *shareResponse){
    //            if (shareResponse.responseCode == UMSResponseCodeSuccess) {
    //                
    //            }
    //        }];
    //    }
    //    self.alpha = 0;
}

//- (void)addguester {
//    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelButtonClicked)];
//    [self addGestureRecognizer:tap];
//}


@end
